package com.atguigu.flink.chapter10.function;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Expressions;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.TableAggregateFunction;
import org.apache.flink.util.Collector;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @Author lzc
 * @Date 2023/6/30 09:08
 */
public class AggTableFunctionDemo {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        tEnv.executeSql("create table sensor(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int " +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 's1', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'properties.group.id' = 'atguigu', " +
                            "  'scan.startup.mode' = 'latest-offset', " +
                            "  'format' = 'csv' " +
                            ")");
        
        Table table = tEnv.from("sensor");
        
        // 把字符变成大写的功能
        
        // 1. 注册一个自定义函数
        tEnv.createTemporaryFunction("top2", Top2.class);
        table
            .groupBy($("id"))
            .flatAggregate(Expressions.call("top2", $("vc")))
            .select($("id"), $("rank"), $("vc"))
            .execute()
            .print();
        
        // 3. 在 sql 中使用
        // 不支持
        
    }
    
    public static class Top2 extends TableAggregateFunction<Result, FirstSecond> {
        
        // 初始化累计器
        @Override
        public FirstSecond createAccumulator() {
            return new FirstSecond();
        }
        
        // 进行累加
        public void accumulate(FirstSecond fs, Integer vc) {
            if (vc > fs.first) {
                fs.second = fs.first;
                fs.first = vc;
            } else if (vc > fs.second) {
                fs.second = vc;
            }
        }
        
        // 把结果输出
        // 方法名: emitValue
        // 参数 1: 累计器
        // 参数 2: 收集器
        public void emitValue(FirstSecond fs, Collector<Result> out) {
            out.collect(new Result("第一名", fs.first));
            if (fs.second > Integer.MIN_VALUE) {
                out.collect(new Result("第二名", fs.second));
            }
        }
    }
    
    public static class Result {
        public String rank;
        public Integer vc;
        
        public Result(String rank, Integer vc) {
            this.rank = rank;
            this.vc = vc;
        }
    }
    
    public static class FirstSecond {
        public Integer first = Integer.MIN_VALUE;
        public Integer second = Integer.MIN_VALUE;
        
        
    }
    
    
}
/*
30
    第一名   30

40
    第一名   40
    第二名   30
    
35
   第一名   40
   第二名   35
    

    
 */